﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CONTROLLER;
using ENTITY;
using DevExpress.XtraEditors;

namespace frm_view
{
    public partial class frmView : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        /// <summary>
        /// Khởi tạo các CONTROLLER
        /// </summary>
         ctrls_MonHoc obj_MonHoc = new ctrls_MonHoc();
        ctrls_Chuong obj_Chuong = new ctrls_Chuong();
        ctrls_Bai obj_Bai = new ctrls_Bai();
        ctrls_Muc obj_Muc = new ctrls_Muc();

        public frmView()
        {
            InitializeComponent();
 
            
        }
        /// <summary>
        /// Hiển thị treeview 
        /// </summary>
        void HienThiTree()
        { 
                        string MaMon,TenMon;
                        string MaChuong, TenChuong;
                        string MaBai,TenBai;
                        string MaMuc,TenMuc;
                        DataTable dt = obj_MonHoc.MonHoc_Select_ALL();
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            MaMon = dt.Rows[i]["MaMon"].ToString();
                            TenMon = dt.Rows[i]["TenMon"].ToString();
                            treeView.Nodes.Add(MaMon, TenMon).Tag = MaMon;
                            
                            DataTable dt1 = obj_Chuong.GetData_SelectSTR("SELECT * FROM tblChuong Where MaMon = '"+MaMon+"'");
                            for (int j = 0; j < dt1.Rows.Count; j++)
                            { 
                                MaChuong = dt1.Rows[j]["MaChuong"].ToString();
                                TenChuong = dt1.Rows[j]["TenChuong"].ToString();
                                treeView.Nodes[MaMon].Nodes.Add(MaChuong, TenChuong).Tag=MaChuong;
            
            
                                DataTable dt2 = obj_Bai.Bai_SelectSQL("SELECT * FROM tblBai Where MaChuong = '"+MaChuong+"'");
                                for (int k = 0; k < dt2.Rows.Count; k++)
                                {
                                    MaBai = dt2.Rows[k]["MaBai"].ToString();
                                    TenBai = dt2.Rows[k]["TenBai"].ToString();
                                    treeView.Nodes[MaMon].Nodes[MaChuong].Nodes.Add(MaBai, TenBai).Tag=MaBai;


                                    DataTable dt3 = obj_Muc.Muc_SelectSQL("SELECT * FROM tblMuc Where Mabai ='"+MaBai+"'");
                                    for (int l = 0; l < dt3.Rows.Count; l++)
                                    {
                                        MaMuc = dt3.Rows[l]["MaMuc"].ToString();
                                        TenMuc = dt3.Rows[k]["TenMuc"].ToString();
                                        treeView.Nodes[MaMon].Nodes[MaChuong].Nodes[MaBai].Nodes.Add(MaMuc, TenMuc).Tag = MaMuc;



                                    }
           

                                }
           
                            }
                        }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            HienThiTree();
            btnMedia.Hide();
        }
        
        /// <summary>
        /// Hiển Thị Nội dung, video
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static string linkMedia;
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            btnMedia.Hide();
            string FullPath;
            string MaChuong, MaBai,MaMuc;
            //Hiển Thị nội dung Từ Chương
            DataTable dt = obj_Chuong.GetData_SelectAll();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                MaChuong = dt.Rows[i]["MaChuong"].ToString();

                if (treeView.SelectedNode.Tag.ToString() == MaChuong)
                {
                    FullPath = dt.Rows[i]["NoiDung"].ToString();

                    try
                    {

                        if (!string.IsNullOrEmpty(FullPath))
                            rtbContent.LoadDocument(@"NoiDung\" + FullPath);

                    }
                    catch
                    {
                        XtraMessageBox.Show("Đường Dẫn hoặc đuôi file không hợp lệ");
                    }
                    try
                    {
                        linkMedia = dt.Rows[i]["Video"].ToString();
                        if (!string.IsNullOrEmpty(linkMedia))
                            btnMedia.Show();
                    }
                    catch (Exception)
                    {


                    }

                    






















                    
                }

            }
            //Hiển Thị nội dung Từ Bài
            DataTable dt1 = obj_Bai.Bai_SelectALL();
            for (int j = 0; j < dt1.Rows.Count; j++)
            {
                MaBai = dt1.Rows[j]["MaBai"].ToString();
                if (treeView.SelectedNode.Tag.ToString() == MaBai)
                {
                    FullPath = dt1.Rows[j]["NoiDung"].ToString();
                 
                    try
                    {
                        
                        if (!string.IsNullOrEmpty(FullPath))
                            rtbContent.LoadDocument(@"NoiDung\" + FullPath);
                      
                    }
                    catch
                    {
                        XtraMessageBox.Show("Đường Dẫn hoặc đuôi file không hợp lệ");
                    }
                    try
                    {
                           linkMedia = dt.Rows[j]["Video"].ToString();
                        if (!string.IsNullOrEmpty(linkMedia))
                            btnMedia.Show();
                    }
                    catch (Exception)
                    {
                        
                        
                    }
                }
            }

           //HIen thi noi dung Muc
             DataTable dt2 = obj_Muc.Muc_SelectALL();
             for (int k = 0; k < dt2.Rows.Count; k++)
             {
                 MaMuc = dt2.Rows[k]["MaMuc"].ToString();
                 if (treeView.SelectedNode.Tag.ToString() == MaMuc)
                 {
                     FullPath = dt2.Rows[k]["NoiDung"].ToString();

                     try
                     {

                         if (!string.IsNullOrEmpty(FullPath))
                             rtbContent.LoadDocument(@"NoiDung\" + FullPath);

                     }
                     catch
                     {
                         XtraMessageBox.Show("Đường Dẫn hoặc đuôi file không hợp lệ");
                     }
                     try
                     {
                         linkMedia = dt2.Rows[k]["Video"].ToString();
                         if (!string.IsNullOrEmpty(linkMedia))
                             btnMedia.Show();
                     }
                     catch (Exception)
                     {


                     }
                 }
             }



            //DataTable dt = obj_Chuong.GetData_SelectAll();
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    MaChuong = dt.Rows[i]["MaChuong"].ToString();
            //    if (treeView.SelectedNode.Tag.ToString() == MaChuong)
            //    {
            //        rtbContent.ClearUndo();
            //        FullPathChuong = dt.Rows[i]["NoiDung"].ToString();
            //        rtbContent.LoadDocument("" + FullPathChuong);
            //    }
            //    //Code Doc child Node :(
            //    DataTable dt1 = obj_Bai.Bai_SelectSQL("SELECT TenBai FROM tblBai MaChuong ='" + MaChuong + "'");
            //    for (int j = 0; j < dt1.Rows.Count; j++)
            //    {
            //        MaBai = dt1.Rows[j]["MaBai"].ToString();
            //        if (treeView.SelectedNode.Tag.ToString() == MaBai)
            //        {
            //            rtbContent.ClearUndo();
            //            FullPathBai = dt1.Rows[j]["NoiDung"].ToString();
            //            rtbContent.LoadDocument("" + FullPathBai);
            //        }
            //    }


            //}
            
        }


        /// <summary>
        /// Tham số toàn cục nhầm lấy giá trị từ method tstrHide_Click
        /// </summary>
        bool hideImage;
        /// <summary>
        /// Click ẩn hiện chuột
        /// </summary>
        /// <param name="sender">stripImges_Click</param>
        /// <param name="e"></param>
        private void stripImges_Click(object sender, EventArgs e)
        {
            if (hideImage == false)
            {

                pelBanner.Hide();
                pelFooter.Hide();
                hideImage = true;
    
            }

            else
            {
                hideImage = false;
                pelBanner.Show();
                pelFooter.Show();
             
            }
        }

       
        /// <summary>
        /// Ẩn hiện Cây thư mục
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void strTreeview_Click(object sender, EventArgs e)
        {

            splitContainer.SplitterDistance = 0;


        }

        private void tsbAdmin_Click(object sender, EventArgs e)
        {
            frmAdmin frm = new frmAdmin();
            frm.Show();
        }

        private void btnMedia_Click(object sender, EventArgs e)
        {
            Media media = new Media();
            media.Show();
        }
    }
}
